package com.twl.app.base.ctrl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.twl.app.base.bo.AdjuestBoImpl;
import com.twl.app.base.entity.Checktemplet;
import com.twl.app.base.entity.Leavebmoney;
import com.twl.app.base.entity.Loyalward;
import com.twl.app.base.entity.Moneyadjust;
import com.twl.app.base.entity.Overtimemeals;
import com.twl.app.base.entity.Overtimemoney;
import com.twl.app.base.entity.Shiftallowance;
import com.twl.app.base.entity.Uemp;
import com.twl.app.base.util.BigdecUitl;
import com.twl.app.base.util.DateComm;
import com.twl.app.base.util.SalParamUtils;
import com.twl.core.framework.ctrl.AbsCrudCtrl;

@Transactional
@Service("adjuestCtrlImpl")
public class AdjuestCtrlImpl extends AbsCrudCtrl<AdjuestBoImpl> {

	private Logger logger = Logger.getLogger(AdjuestCtrlImpl.class);

	@Autowired
	private BigdecUitl bigdecUitl;
	@Autowired
	private CommonCtrlImpl commonCtrlImpl;
	@Autowired
	private DateComm dateComm;

	/**
	 * 工资调整 （正式或者派遣离职-扣减）
	 * 
	 * @param moneyadjust
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings("static-access")
	public String insertMoneyadjust(Checktemplet checktemplet,Uemp uemp) throws Exception {
		try{
			Moneyadjust moneyadjust = new Moneyadjust();
		/*	Uemp uemp = commonCtrlImpl.getUemp(checktemplet.getStaffint());
			if(uemp==null){
				return "no user";
			}*/
			if (new Integer(2).equals(uemp.getUmpstaff())) {// 离职员工
				String stafftype = uemp.getStafftype();// 员工类型 0
				String staffmoney = uemp.getMoneycount(); // 基本工资
				Integer outforworkint = commonCtrlImpl
						.getOutforworkint(uemp.getDatedimi().substring(0, 6));// 应出勤
				Integer realityint = commonCtrlImpl.getOutforworkLZ(checktemplet.getStaffint());// 实际出勤
				// 派遣入职、正式或者派遣离职当月基本工资：工资基数/月应出勤工作日×（月应出勤工作日 - 月实际出勤工作日）
				BigDecimal amountdou = bigdecUitl.mul(
						bigdecUitl.div(new BigDecimal(staffmoney).doubleValue(),
								outforworkint.doubleValue(),10).doubleValue(),
						bigdecUitl.sub(outforworkint.doubleValue(), realityint)
								.doubleValue());
				
				moneyadjust.setStaffint(uemp.getStaffint());
				moneyadjust.setStafftype(stafftype);
				moneyadjust.setStafftypes(uemp.getStafftypes());
				moneyadjust.setStaffmoney(staffmoney);
				moneyadjust.setOutforworkint(outforworkint);
				moneyadjust.setRealityint(realityint.doubleValue());
				BigDecimal amountNew = new BigDecimal(0).subtract(amountdou);//正负号转换
				moneyadjust.setAmountdou(bigdecUitl.formatBigDecimal(amountNew));
				moneyadjust.setDatemoney(dateComm.getDateMoney(uemp, checktemplet));

				/*uemp.setDelstaff(1);
				String result = bo.updateBf(uemp);*/
				return bo.insertMoneyadjust(moneyadjust);
			}
		}catch(Exception e){
			throw e ;
		}
		return "no leave";
	}

	/**
	 * 请假扣款
	 * 
	 * @param leavebmoney
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings("static-access")
	public String insertLeavebmoney(Checktemplet checktemplet,Uemp uemp) throws Exception {
		
		try{
			Leavebmoney leavebmoney = new Leavebmoney();
			/*Uemp uemp = commonCtrlImpl.getUemp(checktemplet.getStaffint());
			if(uemp==null){
				return "no user";
			}*/

			String stafftype = uemp.getStafftype();// 员工类型( 0 正式、 1 派遣员工)
			String staffmoney = uemp.getMoneycount(); // 基本工资

			Integer outforworkint = commonCtrlImpl.getOutforworkint(checktemplet
					.getPeriod());// 应出勤(上月)
			Integer outforworkintNow = commonCtrlImpl
					.getOutforworkintNow(checktemplet.getPeriod());// 本月应出勤

			leavebmoney.setStaffint(checktemplet.getStaffint());
			leavebmoney.setStafftype(stafftype);
			leavebmoney.setStafftypes(uemp.getStafftypes());
			leavebmoney.setStaffmoney(staffmoney);
			leavebmoney.setDatemoney(dateComm.getDateMoney(uemp, checktemplet));
			if ( null!=checktemplet.getSickday() ) {// 有病假()
				// 缺勤扣除工资=工资基数/月应出勤工作日*病假工作日（导入）×病假扣款比例（1 - 本人工资比例）
				Double sick = commonCtrlImpl.getSick(checktemplet); // 病假扣款比例
				String workPlace = uemp.getWorkPlace();//工作地
				//FIXME
				String salarySH = SalParamUtils.getValueByTypeAndKey("SALARY_AVG", "SHANGHAI") ;//待处理,上海市平均工作 
				leavebmoney.setLeavebsick(checktemplet.getSickday());
				leavebmoney.setMoneyscale(sick);
				
				if ("0".equals(stafftype)) {// 正式员工
					BigDecimal amount = bigdecUitl.mul(
							bigdecUitl.mul(
									bigdecUitl.div(
											new BigDecimal(staffmoney)
													.doubleValue(),
											outforworkintNow.doubleValue(),10)
											.doubleValue(),
									checktemplet.getSickday()).doubleValue(), sick);
				
					BigDecimal amountMore = new BigDecimal(String.valueOf(amount));
					if("SHANGHAI".equals(workPlace)){//按上海市平均工资算病假扣款
						BigDecimal amountSH = 
								bigdecUitl.mul(
										bigdecUitl.div(
												bigdecUitl.sub(new BigDecimal(staffmoney).doubleValue(), new BigDecimal(salarySH).doubleValue())
														.doubleValue(),
												outforworkintNow.doubleValue(),10)
												.doubleValue(),
										checktemplet.getSickday());
						int flag = amount.compareTo(amountSH);
						if(!(flag==1)){//小于
							amountMore = amountSH ;
							
						}
					}
					leavebmoney.setOutforworkint(outforworkintNow);
					//BigDecimal amountMore = amountSH>amount ? amountSH:amount;
					leavebmoney.setAmountsick(bigdecUitl.formatBigDecimal(amountMore));
				} else {// 派遣员工
					BigDecimal amount = bigdecUitl.mul(
							bigdecUitl.mul(
									bigdecUitl.div(
											new BigDecimal(staffmoney)
													.doubleValue(),
											outforworkint.doubleValue(),10)
											.doubleValue(),
									checktemplet.getSickday()).doubleValue(), sick);
				
					BigDecimal amountMore = new BigDecimal(String.valueOf(amount));
					if("SHANGHAI".equals(workPlace)){//按上海市平均工资算病假扣款
						BigDecimal amountSH =
								bigdecUitl.mul(
										bigdecUitl.div(
												bigdecUitl.sub(new BigDecimal(staffmoney).doubleValue(), new BigDecimal(salarySH).doubleValue())//添加
														.doubleValue(),
												outforworkintNow.doubleValue(),10)
												.doubleValue(),
										checktemplet.getSickday());
						int flag = amount.compareTo(amountSH);
						if(!(flag==1)){//小于
							amountMore = amountSH ;
						}
					}
					leavebmoney.setOutforworkint(outforworkint);
					leavebmoney.setAmountsick(bigdecUitl.formatBigDecimal(amountMore));
				}
			}
			if ( null != checktemplet.getPersonalday() ) {// 有事假
				// 缺勤工资=工资基数/月应出勤工作日×事假工作日。
				Double thing = checktemplet.getPersonalday();// 事假天数
				leavebmoney.setLeavebthing(thing);

				if ("0".equals(stafftype)) {// 正式员工
					BigDecimal amount = bigdecUitl.mul(
							bigdecUitl.div(
									new BigDecimal(staffmoney).doubleValue(),
									outforworkintNow.doubleValue(),10)
									.doubleValue(), thing);

					leavebmoney.setOutforworkint(outforworkintNow);
					leavebmoney.setAmountthing(bigdecUitl.formatBigDecimal(amount));

				} else {// 派遣
					BigDecimal amount = bigdecUitl.mul(
							bigdecUitl.div(
									new BigDecimal(staffmoney).doubleValue(),
									outforworkint.doubleValue(),10).doubleValue(),
							thing);

					leavebmoney.setOutforworkint(outforworkint);
					leavebmoney.setAmountthing(bigdecUitl.formatBigDecimal(amount));
				}
			}
			if (null != checktemplet.getAbsentday()) {// 有矿工
				// 旷工：缺勤工资=工资基数/月应出勤工作日×旷工工作日
				Double absentday = checktemplet.getAbsentday();// 旷工天数
				leavebmoney.setLeavebint(absentday);

				if ("0".equals(stafftype)) {// 正式员工
					BigDecimal amount = bigdecUitl.mul(
							bigdecUitl.div(
									new BigDecimal(staffmoney).doubleValue(),
									outforworkintNow.doubleValue(),10)
									.doubleValue(), absentday);

					leavebmoney.setOutforworkint(outforworkintNow);
					leavebmoney.setAmountdou(bigdecUitl.formatBigDecimal(amount));

				} else {// 派遣

					BigDecimal amount = bigdecUitl.mul(
							bigdecUitl.div(
									new BigDecimal(staffmoney).doubleValue(),
									outforworkint.doubleValue(),10).doubleValue(),
							absentday);

					leavebmoney.setOutforworkint(outforworkint);
					leavebmoney.setAmountdou(bigdecUitl.formatBigDecimal(amount));
				}
			}
			return bo.insertLeavebmoney(leavebmoney);
		
		}catch(Exception e){
			throw e;
		}
		
		
	}

	/**
	 * 加班工资
	 * 
	 * @param overtimemoney
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings("static-access")
	public String insertOvertimemoney(Checktemplet checktemplet,Uemp uemp) throws Exception {
		try{
			Overtimemoney overtimemoney = new Overtimemoney();
			/*Uemp uemp = commonCtrlImpl.getUemp(checktemplet.getStaffint());
			if(uemp==null){
				return "no user";
			}*/
			Double workovertime = checktemplet.getWorkovertime(); // 工作日加班
			Double restovertime = checktemplet.getRestovertime(); // 周末加班
			Double holidayovertime = checktemplet.getHolidayovertime(); // 节假日加班
			String staffmoney = uemp.getMoneycount(); // 基本工资

			BigDecimal hourMoney = bigdecUitl.div(new BigDecimal(staffmoney).doubleValue(),(8*21.75),10);// 时薪
					/*bigdecUitl.divNotExactly(new BigDecimal(staffmoney).doubleValue(), 8)
							.doubleValue(), 21.75); // 时薪
*/
			overtimemoney.setStaffint(checktemplet.getStaffint());
			overtimemoney.setStafftype(uemp.getStafftype());
			overtimemoney.setStafftypes(uemp.getStafftypes());
			overtimemoney.setStaffmoney(staffmoney);
			overtimemoney.setOvertime(workovertime);
			overtimemoney.setBreakhr(restovertime);
			overtimemoney.setFestvalhr(holidayovertime);
			overtimemoney.setDatemoney(dateComm.getDateMoney(uemp, checktemplet));
				//管理层为 1
			if ("1".equals(uemp.getIsmanage())) { // 部门总经理、副总经理、见习总助、团队经理以及字母级员工仅法定假日加班计加班费，其他时段加班无加班费（标志位)

				if (holidayovertime != null) {
					BigDecimal amount = bigdecUitl.mul(hourMoney.doubleValue(),
							(3 * holidayovertime));
					overtimemoney.setAmountdou(bigdecUitl.formatBigDecimal(amount));
				}

			} else {// 其他员工
				BigDecimal amount = new BigDecimal(0);

				if (workovertime != null) {
					amount = bigdecUitl.add(
							amount.doubleValue(),
							bigdecUitl.mul(hourMoney.doubleValue(),
									(1.5 * workovertime)).doubleValue());

				}
				if (restovertime != null) {
					amount = bigdecUitl.add(
							amount.doubleValue(),
							bigdecUitl.mul(hourMoney.doubleValue(),
									(2 * restovertime)).doubleValue());

				}
				if (holidayovertime != null) {
					amount = bigdecUitl.add(
							amount.doubleValue(),
							bigdecUitl.mul(hourMoney.doubleValue(),
									(3 * holidayovertime)).doubleValue());
				}
				overtimemoney.setAmountdou(bigdecUitl.formatBigDecimal(amount));
			}
			return bo.insertOvertimemoney(overtimemoney);
		
		}catch (Exception e) {
			throw e ;
		}
	}

	/**
	 * 加班餐补
	 * 
	 * @param overtimemeals
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings("static-access")
	public String insertOvertimemeals(Checktemplet checktemplet,Uemp uemp) throws Exception {
		try{
			Overtimemeals overtimemeals = new Overtimemeals();
			/*Uemp uemp = commonCtrlImpl.getUemp(checktemplet.getStaffint());
			if(uemp==null){
				return "no user";
			}*/
			Integer overtimeint = checktemplet.getOvertimemealnum();// 餐补数

			overtimemeals.setStaffint(checktemplet.getStaffint());
			overtimemeals.setStafftype(uemp.getStafftype());
			overtimemeals.setStafftypes(uemp.getStafftypes());
			
			overtimemeals.setDatemoney(dateComm.getDateMoney(uemp, checktemplet));

			if (checktemplet.getOvertimemealnum() != null) {// 是否有餐补
				// 15元/次（参数）×加班餐贴个数
				BigDecimal amountdou = bigdecUitl.mul(new Double(15),
						overtimeint.doubleValue());

				overtimemeals.setOvertimeint(overtimeint);
				overtimemeals.setAmountdou(bigdecUitl.formatBigDecimal(amountdou));
			}
			return bo.insertOvertimemeals(overtimemeals);
		
		}catch (Exception e) {
			throw e ;
		}
	}

	/**
	 * 轮班津贴
	 * 
	 * @param shiftallowance
	 * @return
	 * @throws Exception 
	  */
	@SuppressWarnings("static-access")
	public String insertShiftallowance(Checktemplet checktemplet,Uemp uemp) throws Exception {
		try{
			Shiftallowance shiftallowance = new Shiftallowance();
			/*Uemp uemp = commonCtrlImpl.getUemp(checktemplet.getStaffint());
			if(uemp==null){
				return "no user";
			}*/
			shiftallowance.setStaffint(checktemplet.getStaffint());
			shiftallowance.setStafftype(uemp.getStafftype());
			shiftallowance.setStafftypes(uemp.getStafftypes());
			shiftallowance.setDatemoney(dateComm.getDateMoney(uemp, checktemplet));
			
			if (checktemplet.getCustomerhourtwo() != null || checktemplet.getNightcar() != null) { //有1.2小时加班 为客服或者夜班车贴
			//if ("0".equals(uemp.getStafftypes())) {// "0" 代表客服
				if(checktemplet.getCustomerhourtwo() != 0 || checktemplet.getNightcar() != 0){
					BigDecimal amount = new BigDecimal(0);
					if (checktemplet.getCustomerhourtwo() != null) {// 有1.2小时加班
						Integer customerhourtwo = checktemplet.getCustomerhourtwo();
						shiftallowance.setCustomerhourtwo(customerhourtwo);
						String staffmoney = uemp.getMoneycount(); // 基本工资
						BigDecimal hourMoney = bigdecUitl.div(new BigDecimal(staffmoney).doubleValue(),(8*21.75),10); // 时薪
						/*BigDecimal hourMoney = bigdecUitl.divNotExactly(
								bigdecUitl.divNotExactly(
										new BigDecimal(staffmoney).doubleValue(), 8, 3)
										.doubleValue(), 21.75, 3); // 时薪*/
						amount = bigdecUitl.add(amount.doubleValue(), bigdecUitl.mul(hourMoney.doubleValue(),
								new BigDecimal(customerhourtwo * 0.2).doubleValue()).doubleValue());
					
					}
					if (checktemplet.getNightcar() != null) {
						Integer nightcar = checktemplet.getNightcar(); // 夜班车贴数
						shiftallowance.setEvenumber(nightcar);
						amount = bigdecUitl.add(amount.doubleValue(), new BigDecimal(
								nightcar * 40).doubleValue());
					}
					shiftallowance.setAmountdou(bigdecUitl.formatBigDecimal(amount));
					
				}
			} else if (checktemplet.getCbban() != null && checktemplet.getCbban() != 0) {
				//else if ("2".equals(uemp.getStafftypes())) {// "2" 代表催收
				BigDecimal amount = new BigDecimal(0);
				if (checktemplet.getCbban() != null) {
					Integer CB = checktemplet.getCbban();// CB班
					shiftallowance.setCBstaffnumber(CB);
					amount = bigdecUitl.add(amount.doubleValue(), new BigDecimal(
							CB * 25).doubleValue());
				}
				
				shiftallowance.setAmountdou(bigdecUitl.formatBigDecimal(amount));
			} else {
			//else if ("1".equals(uemp.getStafftypes())) {// "1" 代表授权
				BigDecimal amount = new BigDecimal(0);
				BigDecimal amountmeal = new BigDecimal(0);

				if (checktemplet.getAban() != null) {// A班
					Integer A = checktemplet.getAban();
					shiftallowance.setAstaffnumber(A);

					amount = bigdecUitl.add(amount.doubleValue(), new BigDecimal(
							A * 25).doubleValue());
				}
				if (checktemplet.getBban() != null) {
					Integer B = checktemplet.getBban();// B班
					shiftallowance.setBstaffnumber(B);

					amount = bigdecUitl.add(amount.doubleValue(), new BigDecimal(
							B * 25).doubleValue());
				}
				if (checktemplet.getCban() != null) {
					Integer C = checktemplet.getCban();// C班
					shiftallowance.setCstaffnumber(C);

					amount = bigdecUitl.add(amount.doubleValue(), new BigDecimal(
							C * 125).doubleValue());
					amountmeal = bigdecUitl.add(amountmeal.doubleValue(),
							new BigDecimal(C * 15).doubleValue());// 餐补
					shiftallowance.setAmountmeals(bigdecUitl.formatBigDecimal(amountmeal));
				}
				if (checktemplet.getDban() != null) {
					Integer D = checktemplet.getDban();// D班
					shiftallowance.setDstaffnumber(D);

					amount = bigdecUitl.add(amount.doubleValue(), new BigDecimal(
							D * 200).doubleValue());
					amountmeal = bigdecUitl.add(amountmeal.doubleValue(),
							new BigDecimal(D * 15).doubleValue());// 餐补
					shiftallowance.setAmountmeals(bigdecUitl.formatBigDecimal(amountmeal));
				}
				if (checktemplet.getEban() != null) {
					Integer E = checktemplet.getEban();// E班
					shiftallowance.setEstaffnumber(E);

					amount = bigdecUitl.add(amount.doubleValue(), new BigDecimal(
							E * 25).doubleValue());
				}
				if (checktemplet.getMorningban() != null) {
					Integer morningban = checktemplet.getMorningban();
					shiftallowance.setMeveint(morningban);

					amount = bigdecUitl.add(amount.doubleValue(), new BigDecimal(
							morningban * 25).doubleValue());
				}
				if (checktemplet.getNightban() != null) {
					Integer nightBan = checktemplet.getNightban();
					shiftallowance.setNightban(nightBan);

					amount = bigdecUitl.add(amount.doubleValue(), new BigDecimal(
							nightBan * 25).doubleValue());
				}
				if (checktemplet.getNightcar() != null) {
					Integer nightcar = checktemplet.getNightcar();
					shiftallowance.setEvenumber(nightcar);

					amount = bigdecUitl.add(amount.doubleValue(), new BigDecimal(
							nightcar * 25).doubleValue());
				}
				shiftallowance.setAmountdou(bigdecUitl.formatBigDecimal(amount));

			} 

			return bo.insertShiftallowance(shiftallowance);
		}catch(Exception e){
			throw e ;
		}

	}

	/**
	 * 忠诚奖
	 * 
	 * @param loyalward
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings("static-access")
	public String insertLoyalward(Checktemplet checktemplet,Uemp uemp) throws Exception {
		try{
			Loyalward loyalward = new Loyalward();
			/*Uemp uemp = commonCtrlImpl.getUemp(checktemplet.getStaffint());
			if(uemp==null){
				return "no user";
			}*/
			//FIXME 
			String nowMonth = new SimpleDateFormat("MM").format(dateComm.getRealDate(checktemplet.getPeriod()));// 当前月01或07

			if (nowMonth.equals("01") || nowMonth.equals("07")) {// 只有在0105或0705发放忠诚奖
				
				String rzDate = uemp.getDateentry(); // 入职时间
				String staffmoney = uemp.getMoneycount(); // 基本工资
				Double personalday = commonCtrlImpl.getPersonalday(checktemplet);// 前六个月的请假数
				Integer outwork = commonCtrlImpl.getoutwork(checktemplet);// 得到前6个月应出勤天数

				loyalward.setStaffint(uemp.getStaffint());
				loyalward.setStafftype(uemp.getStafftype());
				loyalward.setStafftypes(uemp.getStafftypes());
				loyalward.setEntrytime(rzDate);
				loyalward.setStaffmoney(staffmoney);
				loyalward.setLeavebint(personalday);
				loyalward.setDatemoney(dateComm.getDateMoney(uemp, checktemplet));

				if (uemp.getStafftype().equals("0")
						&& (!uemp.getUmpstaff().equals("2"))) {// 正式员工，未离职

					Integer zzMonth = dateComm.calDate(checktemplet,rzDate);// 在职满月份
					
					if (zzMonth > 5) {// 在职满6个月
						new BigDecimal(staffmoney);
						BigDecimal amount = bigdecUitl.mul(
								new BigDecimal(staffmoney).doubleValue(),
								bigdecUitl.div(
										bigdecUitl.sub(outwork.doubleValue(),
												personalday).doubleValue(),
										outwork.doubleValue(),10).doubleValue());
						// 需加解密
						loyalward.setAmountdou(bigdecUitl.formatBigDecimal(amount));
					} else {
						BigDecimal amount = BigdecUitl.mul(
								bigdecUitl.mul(
										new BigDecimal(staffmoney).doubleValue(),
										bigdecUitl.div(
												bigdecUitl.sub(
														outwork.doubleValue(),
														personalday).doubleValue(),
												outwork.doubleValue(),10)
												.doubleValue()).doubleValue(),
								BigdecUitl.div(zzMonth.doubleValue(),
										new Double(6),10).doubleValue());
						
						loyalward.setAmountdou(bigdecUitl.formatBigDecimal(amount));
					}
				}
				return bo.insertLoyalward(loyalward);
			}
		}catch(Exception e){
			throw e ;
		}
		

		return "not Jan or July  ";
	}

	

}
